
__author__ = "Chengye zhao (chengyezhao@gmail.com)"

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from product import Product, to_str
from photo import Photo
import logging

class DeletePhotosJob(webapp.RequestHandler):
	def get(self):
		#build url hash map
		url_dict = {}
		q = db.GqlQuery("SELECT * from Product")
		for product in q:
			url_dict[product.imageBig] = True

		q = db.GqlQuery("SELECT * from Photo")
		for photo in q:
			if not url_dict.has_key(photo.title):
				photo.delete()
		logging.info('Delete useless photos')

application = webapp.WSGIApplication(
	[('/delete_photos', DeletePhotosJob)],
	debug=True)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
	main()
